﻿Imports System
Imports System.Collections.ObjectModel

Namespace System.Workflow.ComponentModel
    <Serializable> _
    Public NotInheritable Class WorkflowParameterBindingCollection
        Inherits KeyedCollection([Of] String, WorkflowParameterBinding)
        ' Methods
        Public Sub New(ByVal ownerActivity As Activity)
            If (ownerActivity Is Nothing) Then
                Throw New ArgumentNullException("ownerActivity")
            End If
            Me.ownerActivity = ownerActivity
        End Sub

        Protected Overrides Sub ClearItems()
            If Not Me.ownerActivity.DesignMode Then
                Throw New InvalidOperationException(SR.GetString("Error_CanNotChangeAtRuntime"))
            End If
            MyBase.ClearItems
        End Sub

        Public Function GetItem(ByVal key As String) As WorkflowParameterBinding
            Return MyBase.Item(key)
        End Function

        Protected Overrides Function GetKeyForItem(ByVal item As WorkflowParameterBinding) As String
            Return item.ParameterName
        End Function

        Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As WorkflowParameterBinding)
            If (item Is Nothing) Then
                Throw New ArgumentNullException("item")
            End If
            If Not Me.ownerActivity.DesignMode Then
                Throw New InvalidOperationException(SR.GetString("Error_CanNotChangeAtRuntime"))
            End If
            If MyBase.Contains(item.ParameterName) Then
                Dim binding As WorkflowParameterBinding = MyBase.Item(item.ParameterName)
                index = MyBase.IndexOf(binding)
                Me.RemoveItem(index)
            End If
            MyBase.InsertItem(index, item)
        End Sub

        Protected Overrides Sub RemoveItem(ByVal index As Integer)
            If Not Me.ownerActivity.DesignMode Then
                Throw New InvalidOperationException(SR.GetString("Error_CanNotChangeAtRuntime"))
            End If
            MyBase.RemoveItem(index)
        End Sub

        Protected Overrides Sub SetItem(ByVal index As Integer, ByVal item As WorkflowParameterBinding)
            If (item Is Nothing) Then
                Throw New ArgumentNullException("item")
            End If
            If Not Me.ownerActivity.DesignMode Then
                Throw New InvalidOperationException(SR.GetString("Error_CanNotChangeAtRuntime"))
            End If
            MyBase.SetItem(index, item)
        End Sub


        ' Fields
        Private ownerActivity As Activity
    End Class
End Namespace

